package com.smartscreen.attendancemanage.controller;

import com.smartscreen.attendancemanage.dto.FaceRecognitionCheckInRequest;
import com.smartscreen.attendancemanage.dto.ManualCheckInRequest;
import com.smartscreen.attendancemanage.entity.AttendanceRecord;
import com.smartscreen.attendancemanage.service.AttendanceService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/attendance")
public class AttendanceController {
    
    @Autowired
    private AttendanceService attendanceService;
    
    @PostMapping("/check-in/manual")
    public ApiResponse<Map<String, String>> manualCheckIn(
            @RequestBody ManualCheckInRequest request) {
        String recordId = attendanceService.manualCheckIn(request);
        Map<String, String> data = new HashMap<>();
        data.put("recordId", recordId);
        return new ApiResponse<>(200, "签到成功", data);
    }
    
    @PostMapping("/check-in/face-recognition")
    public ApiResponse<Map<String, Object>> faceRecognitionCheckIn(
            @RequestBody FaceRecognitionCheckInRequest request) {
        Map<String, Object> result = attendanceService.faceRecognitionCheckIn(request);
        return new ApiResponse<>(200, "识别成功，张三同学已签到", result);
    }
    
    @GetMapping("/reports/class/{classId}")
    public ApiResponse<Map<String, Object>> getClassAttendanceReport(
            @PathVariable String classId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        Map<String, Object> report = attendanceService.getClassAttendanceReport(classId, startDate, endDate);
        return ApiResponse.success(report);
    }
    
    @GetMapping("/records/student/{studentId}")
    public ApiResponse<List<AttendanceRecord>> getStudentAttendanceRecords(
            @PathVariable String studentId,
            @RequestParam String month) {
        List<AttendanceRecord> records = attendanceService.getStudentAttendanceRecords(studentId, month);
        return ApiResponse.success(records);
    }
}